import * as step from "@flow/step"
import process from "node:process"
import {installJdk, installMaven} from "./install"
import {runUserScript} from "./script";

const ENV_NAME_JDK_VERSION: string = "CI_RUNTIME_VERSION"
const ENV_NAME_MAVEN_VERSION: string = "MAVEN_VERSION"

async function runStep(): Promise<void> {
    const inputJdkVersion: string | undefined = process.env[ENV_NAME_JDK_VERSION]
    const inputMavenVersion: string | undefined = process.env[ENV_NAME_MAVEN_VERSION]

    if (inputJdkVersion == undefined) {
        step.error("Install jdk failed, missing jdk version")
        throw new Error("missing jdk version")
    }

    if (inputMavenVersion == undefined) {
        step.error("Install jdk failed, missing maven version")
        throw new Error("missing maven version")
    }

    // 安装命令 Jdk 和 Maven
    await installJdk(inputJdkVersion)
    await installMaven(inputMavenVersion)

    // 运行用户命令
    const exitCode = await runUserScript()
    if (exitCode != 0) {
        throw new Error(`run user script failed, exit code: ${exitCode}`)
    }
}


runStep()
    .then(function () {
        step.info("run step successfully!")
    })
    .catch(function (err: Error) {
        step.error(err.message)
        process.exit(-1)
    })